//
//  ACAnchorRoomBottomView.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/30.
//

#import "ACAnchorRoomBottomView.h"

@interface ACAnchorRoomBottomView ()
@property(nonatomic,strong)UIStackView *stackView;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *imageNameArray;
@property(nonatomic,strong)NSMutableArray*btnArray;
@property(nonatomic,assign)NSInteger tagNum;
@end

@implementation ACAnchorRoomBottomView

-(instancetype)initWithTitles:(NSArray *)titles imageNameArray:(NSArray *)imageNameArray tagNum:(NSInteger) tagNum{
    if (self = [super init]) {
        self.titleArray = titles;
        self.imageNameArray = imageNameArray;
        self.btnArray = [NSMutableArray array];
        self.tagNum = tagNum;
    }
    return [self init];
}

-(instancetype)init{
    if (self = [super init]) {
        [self setUI];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    for (int i = 0; i < self.btnArray.count; i ++ ) {
        [self.btnArray[i] wgk_setImagePosition:LXMImagePositionTop spacing:0];
    }
}
-(void)setUI{
    self.stackView = [[UIStackView alloc] init];
    self.stackView.axis = UILayoutConstraintAxisHorizontal;
    self.stackView.alignment = UIStackViewAlignmentFill;
    self.stackView.distribution = UIStackViewDistributionFillEqually;
    [self addSubview:self.stackView];
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 0, 0));
    }];

    for (int i = 0; i < self.titleArray.count; i ++ ) {
        NSDictionary *titleDic = self.titleArray[i];
        NSDictionary *imageDic = self.imageNameArray[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [btn setImage:[UIImage imageWithName:imageDic[@"narmal"] bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageWithName:imageDic[@"selected"] bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateSelected];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:10]];
        [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateSelected];
        [btn setTitle:titleDic[@"narmal"] forState:UIControlStateNormal];
        [btn setTitle:titleDic[@"selected"] forState:UIControlStateSelected];
        btn.tag = _tagNum + i;
        [self.btnArray addObject:btn];
        [self.stackView addArrangedSubview:btn];
        @weakify(self)
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            x.selected = !x.selected;
            if (self.clickActionHandler) {
                self.clickActionHandler(x.tag);
            }
        }];
    }
}
@end
